﻿using Corina.Cydee.DataService;
using Corina.Datamodel.Entities;
using System;
using System.Windows.Controls;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class MainViewmodel : DataContainer
    {
        public MainViewmodel()
        {
            AccountChanged += CurrentAccountChanged;
            CoreClient.GetFetchedTopurlCompleted += GetFetchedTopurlCompleted;
            CoreClient.GetFetchedUrlCompleted += GetFetchedUrlCompleted;
        }

        #region Methods
        private void GetFetchedUrlCompleted(object sender, GetFetchedUrlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                WeburlCollection = new ObservableCollection<WeburlInfo>(e.Result.Entities.OfType<WeburlInfo>());
                SelectedTopurl = TopurlCollection.FirstOrDefault();
            }
        }

        private void GetFetchedTopurlCompleted(object sender, GetFetchedTopurlCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                TopurlCollection = new ObservableCollection<WeburlTopInfo>(e.Result.Entities.OfType<WeburlTopInfo>());
                CoreClient.GetFetchedUrlAsync(CurrentAccount.Account, CurrentAccount.Password);
            }
        }

        private void CurrentAccountChanged(object sender, EventArgs e)
        {
            if (CurrentAccount != null)
            {
                CoreClient.GetFetchedTopurlAsync(CurrentAccount.Account, CurrentAccount.Password);
            }
        }
        #endregion

        #region Navigations
        public IEnumerable<WeburlInfo> Navigations
        {
            get
            {
                if (SelectedTopurl == null)
                {
                    return null;
                }
                return WeburlCollection.Where(t => t.TopurlId == SelectedTopurl.Id);
            }
        }

        #region Urls
        private ObservableCollection<WeburlTopInfo> _topurlCollection;
        public ObservableCollection<WeburlTopInfo> TopurlCollection
        {
            get { return _topurlCollection; }
            set
            {
                _topurlCollection = value;
                RaisePropertyChanged(() => TopurlCollection);
            }
        }

        private WeburlTopInfo _selectedTopurl;
        public WeburlTopInfo SelectedTopurl
        {
            get { return _selectedTopurl; }
            set
            {
                _selectedTopurl = value;
                RaisePropertyChanged(() => SelectedTopurl);
                RaisePropertyChanged(() => Navigations);
            }
        }

        private bool _showAllUrls = false;
        public bool ShowAllUrls
        {
            get { return _showAllUrls; }
            set
            {
                _showAllUrls = value;
                RaisePropertyChanged(() => ShowAllUrls);
            }
        }

        private ObservableCollection<WeburlInfo> _weburlCollection;
        public ObservableCollection<WeburlInfo> WeburlCollection
        {
            get { return _weburlCollection; }
            set
            {
                _weburlCollection = value;
                RaisePropertyChanged(() => WeburlCollection);
            }
        }

        private WeburlInfo _selectedWeburl;
        public WeburlInfo SelectedWeburl
        {
            get { return _selectedWeburl; }
            set
            {
                if (value != null && _selectedWeburl != value)
                {
                    if (SelectedTopurl.Id != value.TopurlId)
                    {
                        SelectedTopurl = TopurlCollection.FirstOrDefault(t => t.Id == value.TopurlId);
                    }
                }
                _selectedWeburl = value;
                RaisePropertyChanged(() => SelectedWeburl);
                if (SelectedWeburl != null)
                {
                    ViewUrl = SelectedWeburl.Url;
                }
                ShowAllUrls = false;
            }
        }
        #endregion

        public override void GotoView(string code)
        {
            if (WeburlCollection.Count(t => t.PageCode == code) > 0)
            {
                SelectedWeburl = WeburlCollection.FirstOrDefault(t => t.PageCode == code);
            }
        }

        #endregion

        #region Page Control
        public ContentControl ElementView
        {
            get { return Helpers.XamlHelper.LoadXamlFromUri<ContentControl>("/Corina.Cydee.Apphub;component/HomeUI/default.xaml"); }
        } 
        #endregion

        #region Images
        public IEnumerable<string> ImageCollection
        {
            get
            {
                for (var i = 1; i < 16; i++)
                {
                    yield return "http://" + WebServiceAddress + "Photos_Files/img" + i.ToString().PadLeft(2, '0') + ".jpg";
                }
            }
        }
        #endregion
    }
}
